package cn.yi.medicalsys.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.yi.medicalsys.pojo.User;
import cn.yi.medicalsys.service.UserService;
import cn.yi.medicalsys.service.impl.UserServiceImpl;

public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	//通过编号查询用户
	public void findUserByNo(HttpServletRequest request, HttpServletResponse response,Integer userno) throws IOException{
		PrintWriter out = response.getWriter();
		UserService user=new UserServiceImpl();
		User use=user.findUserByNo(userno);
		out.print(JSON.toJSONString(use));
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String action=request.getParameter("action");
		String uid=request.getParameter("uid");
		if(action!=null && !action.equals("")){
			if(action.equals("showUserByNo")){				
				Integer userno=null;
				if(uid!=null && !uid.equals("")){
					userno=Integer.valueOf(uid);
				}
				this.findUserByNo(request, response, userno);
			}
		}else{
			out.print("<script>alert('action 为空');history.back()</script>");
		}
		
		
		out.flush();
		out.close();
	}

	public void init() throws ServletException {
	}

}
